/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import javax.servlet.http.HttpServletRequest;

import org.sng.util.var.SString;

public class NodeVariable extends Node {
	
	public NodeVariable()
	{
		super();
	}
	
	public NodeVariable(int n)
	{
		super(n);
	}
	
	public NodeVariable(String id)
	{
		super();
		
		this.PushProperty("id", id);
	}
	
	public NodeVariable(String id, String value)
	{
		super();
		
		this.PushProperty("id", id);
		this.PushProperty("value", value);
	}
	
	public NodeVariable Create()
	{
		return new NodeVariable();
	}
	
	public NodeVariable Create(int n)
	{
		return new NodeVariable(n);
	}
	
	public NodeVariable Create(String id, RuntimeEnvironment re)
	{
		HttpServletRequest request = re.GetRequest();
		
		NodeVariable o = new NodeVariable();
		
		o.m_properties.put("id", new SString(id));
		
		o.m_properties.put("value", new SString(request.getParameter(id + "_value")));
		
		return o;
	}
	
	public String GetType()
	{
		return "variable";
	}
	
	public StringBuffer Do(RuntimeEnvironment re)
	{
		String sid = this.GetProperty("id").GetString(re);
		
		re.AppendJavascript("var " + sid + "_value=\"" + this.GetProperty("value").GetString(re) + "\";");
		
		return new StringBuffer();
	}
	
	public void AppendPostList(RuntimeEnvironment re) {
		String sid = this.GetProperty("id").GetString(re);
		
		re.AppendPostList("para.add(\"cid\", \"" + sid + "\");");
		re.AppendPostList("para.add(\"type\", \"variable\");");
		re.AppendPostList("para.add(\"" + sid + "_value\", " + sid + "_value);");
	}
	
	public void SetValue(RuntimeEnvironment re, int n)
	{
		this.m_properties.put("value", new SString(String.valueOf(n)));
		
		re.AppendJavascript(this.GetProperty("id").GetString(re) + "_value=\"" + this.GetProperty("value").GetString(re) + "\";");
	}
	
	public void SetValue(RuntimeEnvironment re, String s)
	{
		this.m_properties.put("value", new SString(s));
		
		re.AppendJavascript(this.GetProperty("id").GetString(re) + "_value=\"" + this.GetProperty("value").GetString(re) + "\";");
	}
	
	public String GetValue(RuntimeEnvironment re)
	{
		return this.GetProperty("value").GetString(re);
	}
	
	public int GetInt(RuntimeEnvironment re)
	{
		return this.GetProperty("value").GetInt(re);
	}
	
	public String GetString(RuntimeEnvironment re)
	{
		return this.GetProperty("value").GetString(re);
	}

}
